﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PsychveyApp1.classes;

namespace PsychveyApp1
{
    public partial class Signup1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUserSignup_Click(object sender, EventArgs e)
        {
            if (CheckBoxBasic.Checked == true)
            {
                UserAccount user = new UserAccount();
                user.userFullName = tbxUserFullName.Text;
                user.username = tbxUsername.Text;
                user.password = tbxUserPassword.Text;
                user.retypePassword = RetypeUserPassword.Text;
                user.email = tbxUserEmail.Text;

                user.userID = "PSUser_" + user.username;
                user.expirationDateForPremium = "";
                user.accountTypeSelected = "Basic";
                user.paymentStatus = "";
                user.checkAdministratorStatus = "false";
                user.paymentID = "PaymentOf_" + user.username;
                user.receiptID = "Receipt_" + user.username;
                user.planID = "01";

                //Check if the new user name exists in database
                UserAccount u = DBManager.GetUserById(user.userID);
                if (u.userID == null)//the new user name is available
                {
                    DBManager.InsertUser(user);
                    CurrentMaxIDNumber currentMax = new CurrentMaxIDNumber();
                    currentMax.userID = user.userID;
                    currentMax.currentMaxSurveyID = 0;
                    currentMax.currentMaxQuestionID = 0;
                    currentMax.currentMaxAnswerID = 0;
                    currentMax.currentMaxConditionID = 0;
                    DBManager.InsertCurrentMaxIDNumber(currentMax);

                    string username = tbxUsername.Text;
                    string password = tbxUserPassword.Text;
                    string accountTypeSelected = user.accountTypeSelected;
                    string expirationDateForPremium = user.expirationDateForPremium;

                    string querystring = "username=" + username;
                    querystring += "&" + "password=" + password;
                    querystring += "&" + "accounttypeselected=" + accountTypeSelected;
                    querystring += "&" + "expirationdateforpremium=" + expirationDateForPremium;

                    Server.Transfer("SignupSuccessful.aspx?" + querystring);
                }
                else
                { //the new user name already exists in database
                    lblMessage.Text = "The username already exists. Please choose another username.";
                }
            }

            else if (CheckBoxPremium.Checked == true)
            {
                UserAccount user = new UserAccount();
                user.userFullName = tbxUserFullName.Text;
                user.username = tbxUsername.Text;
                user.password = tbxUserPassword.Text;
                user.retypePassword = RetypeUserPassword.Text;
                user.email = tbxUserEmail.Text;

                user.userID = "PSUser_" + user.username;
                user.expirationDateForPremium = "";
                user.accountTypeSelected = "Premium";
                user.paymentStatus = "";
                user.checkAdministratorStatus = "true";
                user.paymentID = "PaymentOf_" + user.username;
                user.receiptID = "Receipt_" + user.username;
                user.planID = "02";
                //Check if the new user name exists in database
                UserAccount u = DBManager.GetUserById(user.userID);
                if (u.userID == null)//the new user name is available
                {
                    DBManager.InsertUser(user);
                    CurrentMaxIDNumber currentMax = new CurrentMaxIDNumber();
                    currentMax.userID = user.userID;
                    currentMax.currentMaxSurveyID = 0;
                    currentMax.currentMaxQuestionID = 0;
                    currentMax.currentMaxAnswerID = 0;
                    currentMax.currentMaxConditionID = 0;
                    DBManager.InsertCurrentMaxIDNumber(currentMax);

                    string username = tbxUsername.Text;
                    string password = tbxUserPassword.Text;
                    string accountTypeSelected = user.accountTypeSelected;
                    string expirationDateForPremium = user.expirationDateForPremium;

                    string querystring = "username=" + username;
                    querystring += "&" + "password=" + password;
                    querystring += "&" + "accounttypeselected=" + accountTypeSelected;
                    querystring += "&" + "expirationdateforpremium=" + expirationDateForPremium;

                    Server.Transfer("SignupSuccessful.aspx?" + querystring);
                }
                else
                { //the new user name already exists in database
                    lblMessage.Text = "The username already exists. Please choose another username.";
                }
            }
        }

        protected void CheckBoxBasic_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void CheckBoxPremium_CheckedChanged(object sender, EventArgs e)
        {

        }

            
        }
   }

